import { _decorator, Color, Component, find, log, Node, UIRenderer } from 'cc';
const { ccclass, property } = _decorator;

import ScrollSelectUtil from '../script/poker/utils/ScrollSelectUtil';

/**
 * 示例
 * 选中是为灰色
 * 选择完是绿色
 */
@ccclass('ScollSelectTest')
export class ScollSelectTest extends Component {
    
    private _root: Node | null
    private _scrollSelect: ScrollSelectUtil | null;

    start() {
        this.initRoot();
        this.initEvent();
    }

    private initRoot() {
        // this._root = find("CardsRoot", this.node)
        this._root = this.node
        log("_root=",this._root)
        // this._scrollSelect = this.node.getComponent(ScrollSelectUtil)
        
    }

    private initEvent() {
        this._scrollSelect.OnStart
        this._scrollSelect.OnMoving
        this._scrollSelect.OnEnd
        // this._scrollSelect.OnStart = () => { this.OnChooseStart() }
        // this._scrollSelect.OnMoving = (nodes: Array<Node>) => { this.OnChooseCard(nodes) }
        // this._scrollSelect.OnEnd = (nodes: Array<Node>) => { this.ChooseCardEnd(nodes) }
    }

    private OnChooseStart() {
        let childrens = this.node.children
        for (let index = 0; index < childrens.length; index++) {
            const child = childrens[index];
            child.getComponent(UIRenderer).color = Color.WHITE
            
        }
        
    }


    private OnChooseCard(nodes: Array<Node>) {
        let childrens = this.node.children
        for (let index = 0; index < childrens.length; index++) {
            const child = childrens[index];
            if (nodes.indexOf(child) >= 0) {
                if (child.getComponent(UIRenderer).color.equals(Color.WHITE)) {
                    child.getComponent(UIRenderer).color = Color.GRAY
                }
            }
            else {
                if (child.getComponent(UIRenderer).color.equals(Color.GRAY)) {
                    child.getComponent(UIRenderer).color = Color.WHITE
                }
            }
        }
    }


    private ChooseCardEnd(nodes: Array<Node>) {
        let childrens = this.node.children
        for (let index = 0; index < childrens.length; index++) {
            const child = childrens[index];
            if (nodes.indexOf(child) >= 0) {
                if (!child.getComponent(UIRenderer).color.equals(Color.GREEN)) {
                    child.getComponent(UIRenderer).color = Color.GREEN
                }
            }
            else {
                if (!child.getComponent(UIRenderer).color.equals(Color.WHITE)) {
                    child.getComponent(UIRenderer).color = Color.WHITE
                }
            }
        }
    }

}